package com.apes.scm.voucher.service;

import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.cache.MemoryCache;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 *
 * @author xul
 * @create 2018-10-18 14:31
 */
public class VoucherUtil {

    public static <T extends Plugin> T getBusinessExecutor(Class<T> clazz, String businessType) {
        String key = "business.executor." + clazz.getSimpleName();
        Map cache = (Map) MemoryCache.getInstance().get(key);
        if (cache == null) {
            synchronized (VoucherUtil.class) {
                cache = new HashMap<>();
                for (T executor : SpringManager.getBeansOfType(clazz).values()) {
                    for (String type : executor.getBusinessTypes()) {
                        cache.put(type, executor);
                    }
                }
                MemoryCache.getInstance().put(key, cache);
            }
        }
        return (T) cache.get(businessType);
    }
}
